$(function(){
    var page,searchKey,size,allPage,zhi,can,_index;//这个参数用来传递表格的index;

    //取消删除的弹框
    $('.chang-cancle').click(function(){
        $('.mask-change').hide()
        $('.delete-tiezi').hide()
    });

    //用ajax调用后台接口,论坛列表;     封装成一个函数,,方便多次调用;
    function luntanList( page,searchKey,size )
    {
        $.ajax({
            async:false,
            cache:false,
            type:'post',
            headers: {"Accept-CMS-Token":token},
            url:lunTan_list,
            data:{
                page:page,
                searchKey:searchKey,
                size:size
            },
            dataType:'json',
            success:successFn,
            error:function(){
                console.log('论坛列表接口请求失败')
            }
        });
        return allPage;
    };

    //页面初始化
    luntanList( 1,'',10 );
    getPage(allPage);

    // ajax请求成功后执行该函数
    function successFn(data)
    {
        if(data.resultCode == 200)
        {
            var init=data.value.list;//整个论坛的数据信息;
            var len=init.length;
            allPage = data.count;
            clearTable();

            for(var i=0;i<len;i++)
            {
                $('.lunTan_admin').append('<tr bbsInformationId="'+init[i].bbsInformationId+'"></tr>');

                $('.lunTan_admin tr').eq(i+1).append('<td>'+(i+1)+'</td>');

                $('.lunTan_admin tr').eq(i+1).append('<td>'+init[i].userId+'</td>');

                $('.lunTan_admin tr').eq(i+1).append('<td>'+init[i].nickname+'</td>');

                $('.lunTan_admin tr').eq(i+1).append('<td>'+cutWord(init[i].title)+'</td>');

                $('.lunTan_admin tr').eq(i+1).append('<td>'+cutWord(init[i].content)+'</td>');

                $('.lunTan_admin tr').eq(i+1).append('<td>'+init[i].createTime+'</td>');

                $('.lunTan_admin tr').eq(i+1).append('<td>'+init[i].scannum+'</td>');

                $('.lunTan_admin tr').eq(i+1).append('<td>'+init[i].commentnum+'</td>');

                $('.lunTan_admin tr').eq(i+1).append('<td><a class="ed look-tizi">查看</a><a href="#" class="delete">删除</a></td>');
            };
        }
        else if(data.resultCode == 515)
        {
            reLogin()
        }
        else
        {
            clearTable();
            $('.lunTan_admin').append('<tr><td></td> <td></td> <td>您搜索的内容不存在</td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td></tr>');
            hintUser();
            return allPage = 1;
        };
        console.log('论坛列表接口请求成功!!!!')
    };

    //清空表格当中的数据,并且追加表头
    function clearTable()
    {
        $('.lunTan_admin').html('');
        $('.lunTan_admin').append('<tr></tr>');
        $('.lunTan_admin tr').eq(0).append('<th>编号</th><th>用户ID</th><th>用户昵称</th><th>帖子标题</th><th>摘要</th><th>发布时间</th><th>阅读量</th><th>评论量</th><th>操作</th>');
    };

    // 页面的搜索功能      已经做好!!!
    $('.lunTan-sou').click(function(){
        searchKey = $('input[name = luntan_sou_cont]').val();
        zhi = luntanList( 1,searchKey,10 );
        getPage(zhi);
        // $('input[name = luntan_sou_cont]').val('');
    });

    //当输入文本框中时,,,按下enter键,,,发起搜索,,,
    $(document).keyup(function(event){
        var ev = ev || event;

        if( ev.keyCode == 13 )
        {
            searchKey = $('input[name = luntan_sou_cont]').val();
            zhi = luntanList( 1,searchKey,10 );
            getPage(zhi);
            // $('input[name = luntan_sou_cont]').val('');
        };

    });

    //查看帖子  帖子id,将帖子id作为参数传递给下一个跳转页面;点击查看,查看论坛帖子的相关内容,跳转到查看帖子look_tizi页面;
    $(document).on('click','.look-tizi',function(){
        var x = $('.look-tizi').index(this);//获取到的是在整个页面当中.look-tizi这个类名的index;
        can= $('.lunTan_admin tr').eq(x+1).attr('bbsInformationId');//获取到这一行的用户id,,,作为参数传递给另外一个新页面
        var id = $.getUrlParam('id');
        window.location.href = look_tizi+'?bbsInformationId='+can+'&id='+id;
    });

    //删除帖子      弹框
    $(document).on('click','.delete',function(){
        var x = $('.delete').index(this);//获取到的是在整个页面当中.delete这个类名的index;
        $('.mask-change').show();
        $('.delete-tiezi').show();
        _index=x;
    });

    //删除帖子的弹框,确定删除;
    $('.sure-del-tiezi').click(function (){
        sure_delete_tizi(_index+1);
    });

    //删除帖子的弹框,确定删除;
    function sure_delete_tizi(_index)
    {
        $.ajax({
            type:'post',
            headers: {"Accept-CMS-Token":token},
            url:delete_tizi,
            data:{
                bbsInformationId:$('.lunTan_admin tr').eq(_index).attr('bbsInformationId'),//帖子的id是从后台获取的;是上一个接口上的东西;将这个参数传递给后台;
            },
            dataType:'json',
            success:function (result){
                if(result.resultCode == 200)
                {
                    console.log('删除帖子接口请求成功!!!!')
                }
                else if(result.resultCode == 515)
                {
                    reLogin()
                }
                else
                {
                    alert('接口非200,非515')
                };
            },
            error:function(){
                console.log('删除帖子接口请求失败')
            },
        });
        //删除完之后重新刷新,,,,向后台请求;
        searchKey = $('input[name = luntan_sou_cont]').val();
        zhi = luntanList( 1,searchKey,10 );
        getPage(zhi);
        $('.mask-change').hide();
        $('.delete-tiezi').hide();
    };

    //字符串截取的一个函数,只显示前六个字;
    function cutWord(a)
    {
        if( (a == '') || (a == null) )
        {
            return '';
        }
        else
        {
            return a.substring(0,6)+'......';
        }
    };

    // 为分页插件传递数据
    function getPage(allPage)
    {
        $('.M-box2').pagination({
            pageCount:allPage,
            jump:true,
            coping:true,
            homePage:'首页',
            endPage:'末页',
            prevContent:'上页',
            nextContent:'下页',
            callback:function (api){//当点击页面当中的时候,会出发这个callback函数;;;;
                page = api.getCurrent()+"";//得到的是要跳转到的那一页的页码;
                searchKey = $('input[name = luntan_sou_cont]').val();
                luntanList( page,searchKey,10 );
            }
        });
    };

    // 当后台状态码是515时,让客户重新登录
    function reLogin()
    {
        alert('登录超时,请重新登录!');
        localStorage.clear();
        window.location.href = login_jump;
    }

    // 为二级菜单所对应的页面中设置显示新增,编辑,删除这些按钮...用地址栏对应的id作为判断条件...后台返回来的数据是:新增,编辑,状态,
    var changeId = $.getUrlParam('id');
    var btnStatus = [];
    var userId = localStorage.getItem('陈');
    if(userId == null)
    {
        alert('亲,不好意思,请重新登录!')
        window.location.href = login_jump;
    };
    var information = JSON.parse( base.decode(localStorage.getItem( userId )) );
    var ul_li_permissionlist = information.ul_li_permissionlist;//三级菜单中的数据

    function showBtnStatus()
    {
        flagOuter:for( var i=0;i< ul_li_permissionlist.length;i++)
        {
            for( var j=0;j<ul_li_permissionlist[i].length;j++ )
            {
                if( ul_li_permissionlist[i][j].length > 0 )
                {
                    for( var q=0;q<ul_li_permissionlist[i][j].length;q++ )
                    {
                        if( ul_li_permissionlist[i][j][q].pid == changeId )
                        {
                            btnStatus = ul_li_permissionlist[i][j];
                            break flagOuter;
                        };
                    };
                }
            };
        };
        if( btnStatus.length > 0 )
        {
            for( var x=0;x<btnStatus.length;x++ )
            {
                if( btnStatus[x].permissionName == '删除' )
                {
                    $('.delete').show();
                }
                else
                {
                    $('.delete').hide();
                }
            };
        }
        else
        {
            $('.delete').show();
        };
    };

    if( changeId != null )
    {
        showBtnStatus();
    };
    // console.log(changeId)




});
